<template>
  <div>
    <el-dialog
      :title="ifAdd + '机构'"
      :visible.sync="addDiavisible"
      width="40%"
      :before-close="cancel"
      @opened="beforeopen"
    >
      <el-form
        :model="form"
        ref="formRef"
        :rules="formRules"
        label-width="80px"
        label-position="left"
      >
        <el-form-item label="机构名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="机构编号" prop="code">
          <el-input v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item label="机构地址" prop="placeId">
          <el-cascader
            v-loading="loading"
            v-model="form.placeId"
            :options="placeId"
            @change="handleChange"
            :props="optionProps"
            style="width: 100%"
          ></el-cascader>
        </el-form-item>

		<el-form-item label="机构账号" prop="mechAccount"v-if="isAdd" >
		  <el-input v-model="form.mechAccount"  :disabled="!isAdd" placeholder="(初始密码123456)"></el-input>
		</el-form-item>


		<el-form-item label="机构类型" prop="mechRole"   v-if="isAdd">
		  <el-radio-group v-model="form.mechRole"  v-if="isAdd">
			  <el-radio  v-model="form.mechRole"  v-if="isAdd"  :label="1"  :disabled="!isAdd" >监管机构</el-radio>
			  <el-radio  v-model="form.mechRole"  v-if="isAdd" :label="2" :disabled="!isAdd" >基层医院</el-radio>
		  </el-radio-group>
		</el-form-item>


      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="addSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getAuthPlace, editMechanism } from "@/api/family_doctor/family_doctor";
import { inputValidator,nineValidator, selectValidator } from "@/utils/validate";
export default {
  data() {
    return {
      addDiavisible: false,
      data: "",
      placeData: false,
      loading: false,
      ifAdd: "新增",
	  isAdd:true,
      form: {
        id: "",
        name: "",
        placeId: "",
        code: "",
        status: "",
		mechRole:"2",
		mechAccount:"",

      },
      placeId: [],
      temPlaceId:'',
      optionProps: {
        value: "id",
        label: "name",
        children: "childrenNodes",
        checkStrictly: true,
      },
      formRules: {
        name: inputValidator,
        code: nineValidator,
        placeId: selectValidator,
      },
    };
  },
  created() {},
  methods: {
    //关闭
    cancel() {
      for (var key in this.form) {
        this.form[key] = "";
      }
      this.$refs.formRef.clearValidate();
      this.$emit("transData");
      this.addDiavisible = false;
    },
    //弹窗渲染之前
    beforeopen() {},
    //获取地区
    getPlaceData() {
      this.loading = true;
      getAuthPlace(this.placeData).then((res) => {
        this.loading = false;
        this.recursion(res.data);
        this.placeId = res.data;
      });
    },
    // 递归
    recursion(data) {
      data.forEach((item) => {
        if (item.childrenNodes.length > 0) {
          this.recursion(item.childrenNodes);
        } else {
          item.childrenNodes = undefined;
        }
      });
    },
    //地区选择事件
    handleChange(value) {
      console.log(value);
      this.temPlaceId = value[value.length - 1];
    },
    //提交
    addSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (!valid) return this.$message.error("请输入正确的格式!");
        this.form.placeId = this.temPlaceId
        editMechanism(this.form).then((res) => {
          if (res.code == "AA000000") {
            this.$message.success(res.msg);
          }
          this.cancel();
        });
      });
    },
    //供父组件调用函数
    openDialog(ifAdd, scope) {
      console.log('scope====',scope)
      this.getPlaceData();
      if (ifAdd == true) {
        this.ifAdd = "新增";
        this.isAdd = true;
      } else {
        this.ifAdd = "修改";
        this.isAdd = false;
        this.form.id = scope.id
        this.form.name = scope.name
        this.form.placeId = scope.placeIds.reverse()
        this.temPlaceId = scope.placeId
        this.form.code = scope.code
        this.form.mechRole = scope.mechRole
        this.form.mechAccount = scope.mechAccount
      }
      this.addDiavisible = true;
    },
  },
};
</script>
